package com.shushan.mixedexam.bean;

import android.text.TextUtils;
import android.util.Log;

import com.android.net.bean.NetBaseBean;
import com.shushan.mixedexam.oralcalculation.bean.OralCalculateExerciseBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MutiExerciseBean
 * @Desciption: //多种练习习题数据bean
 * @author: zhangshihao
 * @date: 2018-07-31
 */
public class MutiExerciseBean extends NetBaseBean {

    public int id;
    public String exercises_type;
    public String exercises_level;
    public String exercises_content;
    public String exercises_imgs;
    public String content_type;
    public String outline_detail_ids;
    public int subject_id;
    public String image_size;
    public int outline_detail_id;
    public String voice;
    public String new_word;
    public String answer;
    public int outline_id;
    public int score;
    public int child_collect;
    public int my_answer_id;
    public String my_answer_id_str;
    public int voice_time;//口语练习录音时间

    private List<MutiExerciseOptionBean> optionBeans = new ArrayList<>();
    private List<ExerciseVideoBean> videoBeans = new ArrayList<>();

    public String right_option;
    public int option_result;
    public String my_option = "0";

    //@{口算错题字段：
    public OralCalculateExerciseBean ocExerciseBean;

    @Override
    public void initByJson(JSONObject jsonObject) throws JSONException {
        if(jsonObject == null){
            return;
        }

        right_option = jsonObject.optString("right_option","");
        option_result = jsonObject.optInt("option_result",0);
        my_option = jsonObject.optString("my_option","");

        id = jsonObject.optInt("id",0);
        exercises_type = jsonObject.optString("exercises_type","");
        exercises_level = jsonObject.optString("exercises_level","");

        int imgWidth = 400;
        image_size = jsonObject.optString("image_size","");
        if(!TextUtils.isEmpty(image_size)){
            JSONArray jsonArray = new JSONArray(image_size);
            for (int i = 0;i < jsonArray.length();i++){
                JSONObject json = jsonArray.optJSONObject(i);
                int width = json.optInt("generateWidth");
                if(imgWidth > width){
                    imgWidth = width;
                }
            }
        }
        exercises_imgs = jsonObject.optString("exercises_imgs","");
        if(!TextUtils.isEmpty(exercises_imgs)){
            exercises_imgs = exercises_imgs.replace("{width}",imgWidth + "");
        }

        content_type = jsonObject.optString("content_type","");
        outline_detail_ids = jsonObject.optString("outline_detail_ids","");
        subject_id = jsonObject.optInt("subject_id",0);
        outline_detail_id = jsonObject.optInt("outline_detail_id",0);
        voice = jsonObject.optString("voice","");
        new_word = jsonObject.optString("new_word","");
        answer = jsonObject.optString("answer","");
        outline_id = jsonObject.optInt("outline_id",0);
        score = jsonObject.optInt("score",0);
        child_collect = jsonObject.optInt("child_collect",0);
        my_answer_id = jsonObject.optInt("my_answer_id",0);
        my_answer_id_str = jsonObject.optString("my_answer_id","");
        voice_time = jsonObject.optInt("voice_time",0);

        JSONArray exerciseContentArr = jsonObject.optJSONArray("exercises_content");
        if(exerciseContentArr != null && exerciseContentArr.length() > 0){
            StringBuffer sb = new StringBuffer();
            for(int i=0;i<exerciseContentArr.length();i++) {
                sb.append(exerciseContentArr.optString(i, ""));
            }
            exercises_content = sb.toString();
        }

        JSONArray optionArr = jsonObject.optJSONArray("exercise_option");
        if(optionArr != null && optionArr.length() > 0){
            for(int i=0;i<optionArr.length();i++){
                JSONObject optionObj = optionArr.optJSONObject(i);
                MutiExerciseOptionBean optionBean = new MutiExerciseOptionBean();
                optionBean.initByJson(optionObj);
                optionBeans.add(optionBean);
            }
        }

        JSONArray videoArr = jsonObject.optJSONArray("video");
        if(videoArr != null && videoArr.length() > 0){
            for(int i=0;i<videoArr.length();i++){
                JSONObject videoObj = videoArr.optJSONObject(i);
                ExerciseVideoBean videoBean = new ExerciseVideoBean();
                videoBean.initByJson(videoObj);
                videoBeans.add(videoBean);
            }
        }

        ocExerciseBean = new OralCalculateExerciseBean();
        ocExerciseBean.initByJson(jsonObject);
    }

    public List<MutiExerciseOptionBean> getOptionBeans() {
        return optionBeans;
    }

    public List<ExerciseVideoBean> getVideoBeans() {
        return videoBeans;
    }
}
